<?php

namespace App\Http\Controllers\Innerend;


use App\Models\User;

class SiteTypeController extends BaseController
{
    // 话题
    public function initialize()
    {
        parent::initialize();
        $uid = $this->getData('channel', '', 'trim');
        $url = url('/innerend/site-type/lists?channel='.$uid);

        $this->assign('init_url', $url);
        $this->model = 'SiteType';
    }

    // 父级
    // 添加，修改，删除，列表，停用 操作

    // 列表数据展示
    protected function listTable($lists)
    {
        // 整理
        $trList = [];
        if(empty($lists)){
            return ['list' => $lists, 'tr' => $trList];
        }


        // 展示列表表格
        $channels = ['user_type' => '身份类型', 'fresh_complain' => '投诉类型', 'good_areas' => '擅长领域'];
        $listHtml = $this->adminList();
        foreach($lists as $key => $item){
            // 数据
            $_channel = $channels[$item['channel']] ?? '-';
            $_id      = $item['id'];

            // 操作
            $operate = $listHtml->operate_edit(['id' => $_id], '编辑');
            $operate .= $listHtml->operate_delete(['id' => $_id], '你确定要删除该分类吗？');
            //$operate .= $listHtml->operate_soft_delete(['id' => $_id], '你确定要删除该分类吗？');

            // 页面
            $tr = $listHtml->list_check($item['id']);
            $tr .= $listHtml->list_td($_channel);
            $tr .= $listHtml->list_td($item['name']);
            $tr .= $listHtml->list_td($item['rank']);
            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));
            $tr .= $listHtml->list_td($operate);
            $tr .= $operate;

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return ['list' => $lists, 'tr' => $trList];
    }

    // 列表顶部按钮
    protected function listTopButton()
    {
        $_channel   = $this->getData('channel', null, 'trim');
        $addUrl     = url('/innerend/'.$this->controller.'/add?channel='.$_channel);
        $top_button = '<a href="javascript:;" onclick="onEdit(\''.$addUrl.'\')" class="btn btn-success radius">添加</a> ';
        $this->assign('top_button', $top_button);
    }

    // 列表字段
    protected function list_fields()
    {
        // 'role' => '角色',
        return ['channel' => '类型', 'name' => '名称', 'rank' => '排序', 'ctime' => ['创建时间', 120], 'operate' => ['操作', 250],];
    }

    // 表单页面
    protected function formTable()
    {
        $html = '';
        $info = $this->onShow();
        $form = $this->adminForm();

        // 用户名
        $channels = ['user_type' => '身份类型', 'fresh_complain' => '投诉类型', 'good_areas' => '擅长领域'];
        $_channel = $this->getData('channel', null, 'trim');
        $_channel = empty($info['channel']) ? $_channel : $info['channel'];


        $show    = $form->formShow($channels[$_channel] ?? '-');
        $name    = $form->formInput('name', $info['name'] ?? '', '名称');
        $rank    = $form->formNumber('rank', $info['rank'] ?? '', '排序');
        $channel = $form->formHidden('channel', $_channel);
        $_id     = $form->formHidden('id', $info['id'] ?? '');


        $html .= $form->makeItem('分类', $show);
        $html .= $form->makeItem('名称', $name);
        $html .= $form->makeItem('排序', $rank);
        $html .= $channel;
        $html .= $_id;

        return $html;
    }

    // 排序
    public function onRank()
    {
        $_value = $this->getData('rank', 0, 'intval');
        $this->throwIf(is_null($_value), '请填写排序数字！');

        return $this->changeField('rank', $_value);
    }

    // 列表条件
    protected function listWhere()
    {
        $where   = [];
        $keyword = $this->getData('keyword', null, 'trim');
        $channel = $this->getData('channel', null, 'trim');
        if($channel){
            $where[] = ['channel', '=', $channel];
        }
        if($keyword){
            $where[] = ['name', 'like', "%$keyword%"];
        }

        return $where;
    }

    // 列表搜索
    protected function list_search()
    {
        $request = $this->getData();
        $form    = $this->adminList();
        $keyword = $form->formInput('keyword', $request['keyword'] ?? '', '关键字');
        $search  = ['keyword' => $keyword,];  // keyword
        $this->assign('search', $search);

        $searchHide = ['channel'];
        $this->assign('searchHide', $searchHide);
    }

    // 添加
    protected function _before_add()
    {
        $data = [
            'channel' => $this->getData('channel', null, 'trim'),
            'name'    => $this->getData('name', null, 'trim'),
            'rank'    => $this->getData('rank', null, 'intval'),
        ];

        return $data;
    }

    // 修改
    protected function _before_edit()
    {
        $data = [
            'channel' => $this->getData('channel', null, 'trim'),
            'name'    => $this->getData('name', null, 'trim'),
            'rank'    => $this->getData('rank', null, 'intval'),
        ];

        return $data;
    }

}


